package day13;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.Test;

public class JDBCTest {

	@Test
	public void test1() {
		String searchName = "张";
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Student> list = new ArrayList<Student>();
		// 加载驱动
		try { // DBUtils
			connection = JDBCUtil.getConnection();
			//String sql = "select id,name,age,gender from student where name like '张%' ";
			String sql = "select id,name,age,gender from student where name like ? ";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, searchName + "%");
			resultSet = preparedStatement.executeQuery();
			// 判断下一行有没有，有返回true，并指向这一行，没有返回false
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
